package com.chezhujie.www.controller;

import com.chezhujie.www.domain.Czjcomments;
import com.chezhujie.www.domain.Czjlike;
import com.chezhujie.www.service.CzjcommentsService;
import com.chezhujie.www.service.CzjlikeService;
import com.sys.core.page.Pagination;
import com.sys.core.utils.ConvertObj;
import com.sys.core.utils.UUIDUtils;
import com.sys.user.base.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 *   用户评论点赞
 * Created by 汤伟杰 on 2017/5/23.
 */
@Controller
@RequestMapping("/czjlike")
public class CzjlikeController extends BaseController<Czjlike> {
    @Autowired
    private CzjlikeService czjlikeService;
    @Autowired
    private CzjcommentsService czjcommentsService;

    /** 列表 **/
    @RequestMapping("/list.do")
    public String list(Integer pageNo, Integer pageSize, Czjlike czjlike, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjlike,req,model);
        setCommentsId(model);
        setUserinfoId(model);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(czjlike);
            Pagination pagination = czjlikeService.list(pageNo.intValue(), pageSize.intValue(), params, model);
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取用户评论点赞列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", czjlike);
        return "czjlike/list";
    }
    /**
    * api接口用户评论点赞列表
    * @param pageNo
    * @param pageSize
    * @param req
    * @param resp
    * @param model
    * @throws Exception
    */
    @RequestMapping("/apiList.jn")
    public void apiList(Integer pageNo, Integer pageSize, Czjlike czjlike,
        HttpServletRequest req, HttpServletResponse resp,
        ModelMap model) throws Exception {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(czjlike);
            Pagination pagination = czjlikeService.apilist(pageNo.intValue(), pageSize.intValue(), params, model);
            jsonResult(1,"","",pagination,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"","获取用户评论点赞列表失败!"+e,"",resp);
        }

    }
    /**
     * api接口用户评论点赞新增
     * @param req
     * @param resp
     * @param model
     * @throws Exception
     */
    @RequestMapping("/apiAdd.jn")
    public void apiAdd(@RequestParam(required = true)int userinfoId,
                       @RequestParam(required = true)String commentsId,HttpServletRequest req, HttpServletResponse resp,
                          ModelMap model) throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("status","0");
        try {


            Map<String, Object> map = new HashMap<String, Object>();
            map.put("userinfoId", userinfoId);
            map.put("commentsId", commentsId);
            Czjcomments comments = czjcommentsService.queryById(commentsId);
            if(comments==null){
                jsonResult(0,"","评论不存在！",param,resp);
            }
            int yesorno = 1;
            List<Map<String, Object>> list = czjlikeService.getApiList(map);
            if(list==null||list.size()==0){
                Czjlike czjlike = new Czjlike();
                czjlike.setCreatetime(new Date());
                czjlike.setLikeId(UUIDUtils.getUUID());
                czjlike.setUserinfoId(userinfoId);
                czjlike.setUpdatetime(new Date());
                czjlike.setCommentsId(commentsId);
                czjlike.setYesorno(yesorno);
                comments.setLikeCount(comments.getLikeCount()+1);
                czjlikeService.insert(czjlike);
                czjcommentsService.update(comments);
                param.put("status","1");
                jsonResult(1,"","",param,resp);
                return;

            }
        for (Map<String, Object> objectMap : list) {
            if(objectMap.get("yesorno")!=null&&0==Integer.parseInt(String.valueOf(objectMap.get("yesorno")))){
                Czjlike czjlike = czjlikeService.queryById(String.valueOf(objectMap.get("likeId")));
                czjlike.setYesorno(1);
                czjlikeService.update(czjlike);
            }
        }
            param.put("status","0");
            jsonResult(1,"","",param,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"99","程序异常",param,resp);
        }

    }
    /**
    * api接口用户评论点赞详情
    * @param req
    * @param resp
    * @param model
    * @throws Exception
    */
    @RequestMapping("/apiDetail.jn")
    public void apiDetail(HttpServletRequest req, HttpServletResponse resp,
    ModelMap model) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        Enumeration<String> enumeration = req.getParameterNames();
        if(enumeration.hasMoreElements()){
            String next = enumeration.nextElement();
            params.put(next, req.getParameter(next));
        }

        try {
            Map<String,Object> result = czjlikeService.getApiDetail(params);
            jsonResult(1,"","",result,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"99","获取用户评论点赞详情失败!"+e,"",resp);
        }

    }
    /** api测试页面 **/
    @RequestMapping("/apiTest.do")
    public String apiList(HttpServletRequest req, HttpServletResponse resp,
        ModelMap model) throws Exception {
        model.addAttribute("entity",new HashMap<String,Object>());
        return "czjlike/apiTest";

    }

    /** 保存 **/
    @RequestMapping("/save.do")
    public String save(Czjlike czjlike,
                      HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        try {

            if(StringUtils.isNotBlank(czjlike.getLikeId())){
                czjlikeService.update(czjlike);


            }else{
                czjlike.setLikeId(UUIDUtils.getUUID());
                czjlikeService.insert(czjlike);
            }
            model.addAttribute("success", "保存用户评论点赞成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "保存用户评论点赞失败!详情："+e.getMessage());
        }
        return "redirect:list.do";
    }


    /** 详情 **/
    @RequestMapping("/detail.do")
    public String detail(@RequestParam(required = true)String likeId,Czjlike czjlike, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjlike,req,model);
        setCommentsId(model);
        setUserinfoId(model);
        try {
            Czjlike entity = czjlikeService.queryById(czjlike.getLikeId());
            model.addAttribute("entity", entity);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "查询用户评论点赞出错!详情："+e.getMessage());
        }
        return "czjlike/detail";
    }
    /** 新增或修改 **/
    @RequestMapping("/add.do")
    public String add(Czjlike czjlike, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjlike,req,model);
        setCommentsId(model);
        setUserinfoId(model);
        if(StringUtils.isNotBlank(czjlike.getLikeId())){
            czjlike = czjlikeService.queryById(czjlike.getLikeId());
            model.addAttribute("entity", czjlike);
        }
        return "czjlike/add";
    }
    /** 删除 **/
    @RequestMapping("/delete.do")
    public String delete(@RequestParam(required = true)String likeId,Czjlike czjlike, HttpServletRequest req, HttpServletResponse resp,
                     ModelMap model){
        try {
            czjlikeService.deleteById(czjlike.getLikeId());
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", e.getMessage());
        }
        return "redirect:list.do";
    }
    private void setCommentsId(ModelMap model) {
        Map<String,Object> param = new HashMap<String,Object>();
        List<Map<String,Object>> listCommentsId = czjlikeService.getListCommentsId(param);
        // map.put("name","");
        // map.put("value","");
        model.addAttribute("listCommentsId", listCommentsId);
    }
    private void setUserinfoId(ModelMap model) {
        Map<String,Object> param = new HashMap<String,Object>();
        List<Map<String,Object>> listUserinfoId = czjlikeService.getListUserinfoId(param);
        // map.put("name","");
        // map.put("value","");
        model.addAttribute("listUserinfoId", listUserinfoId);
    }

    private void setMsg(Czjlike czjlike,HttpServletRequest req, ModelMap model) {
        model.addAttribute("failed", req.getParameter("failed"));
        model.addAttribute("success", req.getParameter("success"));
        model.addAttribute("entity", czjlike);
    }
}
